﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using YTSK.Model;
using System.Xml;

namespace YTSK.cSK.BW
{
    class cMakeXML_ZP
    {
        private string _FHR;
        private string _SKR;
        private string _version;
        public cMakeXML_ZP()
        {
            _FHR = cSK.cSKSet.loadsave(SkConfig.复核人);
            _SKR = cSK.cSKSet.loadsave(SkConfig.收款人);
            _version = cSK.cSKSet.loadsave(SkConfig.税控分类版本号);
            if (_version.Length == 0) { throw new Exception("请先设置税控设备参数"); }
        }
        public string MakeXML(string savefile, Customer customer,Rdrecord rd,List<Dictionary<string,object>> l_rds)
        {
            var xml = new XmlDocument();
            var declare = xml.CreateXmlDeclaration("1.0", "GBK", null);
            xml.AppendChild(declare);
            var kp = xml.CreateElement("Kp");
            var version = xml.CreateElement("Verion");
            version.InnerText = "2.0";
            var fpxx = xml.CreateElement("Fpxx");
            var zsl = xml.CreateElement("Zsl");
            zsl.InnerText = "1";
            fpxx.AppendChild(zsl);
            var fpsj = xml.CreateElement("Fpsj");
            var fp = xml.CreateElement("Fp");
            var main = xml.CreateElement("Djh");
            main.InnerText = "1";
            fp.AppendChild(main);
            main = xml.CreateElement("Gfmc");
            main.InnerText = customer.cCusName;
            fp.AppendChild(main);
            main = xml.CreateElement("Gfsh");
            main.InnerText = customer.TaxNo;
            fp.AppendChild(main);
            main = xml.CreateElement("Gfyhzh");
            main.InnerText = customer.Bank.ObjToString() +" "+ customer.BankAccount.ObjToString();
            fp.AppendChild(main);
            main = xml.CreateElement("Gfdzdh");
            main.InnerText = customer.Address.ObjToString() + " " + customer.Phone.ObjToString();
            fp.AppendChild(main);
            main = xml.CreateElement("Bz");
            //main.InnerText = customer["Address"].ObjToString() + customer["Phone"].ObjToString();
            fp.AppendChild(main);
            main = xml.CreateElement("Fhr");
            main.InnerText = _FHR;
            fp.AppendChild(main);
            main = xml.CreateElement("Skr");
            main.InnerText = _SKR;
            fp.AppendChild(main);
            main = xml.CreateElement("Spbmbbh");
            main.InnerText = _version;
            fp.AppendChild(main);
            main = xml.CreateElement("Hsbz");
            main.InnerText = "0";
            fp.AppendChild(main);
            main = xml.CreateElement("Sgbz");
            main.InnerText = "0";
            fp.AppendChild(main);

            main = xml.CreateElement("Spxx");
            int irowcount = 1;
            for (int i = 0; i <l_rds.Count; i++)
            {
                var tori = l_rds[i];
                var t = tori.ToObj<Rdrecords>();
                if (t.cInvName.ObjToString().Length > 0)
                {
                    var d = xml.CreateElement("Sph");
                    var detail = xml.CreateElement("Xh");
                    detail.InnerText = irowcount.ToString();
                    d.AppendChild(detail);
                    detail = xml.CreateElement("Spbm");
                    var cTaxType = tori["cTaxType"].ObjToString();
                    if (cTaxType.Length != 19)
                    { throw new Exception(string.Format("产品[{0}]的税收编码[{1}]不是是19位", t.cInvName, cTaxType)); }
                    detail.InnerText = cTaxType;
                    d.AppendChild(detail);
                    detail = xml.CreateElement("Qyspbm");
                    //detail.InnerText = "";
                    d.AppendChild(detail);
                    detail = xml.CreateElement("Syyhzcbz");
                    //detail.InnerText = "";
                    d.AppendChild(detail);
                    detail = xml.CreateElement("Yhzcsm");
                    //detail.InnerText = "";
                    d.AppendChild(detail);
                    detail = xml.CreateElement("Lslbz");
                    //detail.InnerText = "";
                    d.AppendChild(detail);
                    detail = xml.CreateElement("Spmc");
                    detail.InnerText = t.cInvName;
                    d.AppendChild(detail);
                    detail = xml.CreateElement("Ggxh");
                    detail.InnerText = t.cInvStd;
                    d.AppendChild(detail);
                    detail = xml.CreateElement("Jldw");
                    detail.InnerText = t.cDW;
                    d.AppendChild(detail);
                    var jehs = t.iSumSale.ObjToDecimal();
                    var taxrate = t.iTaxRateSale.ObjToDecimal();
                    var jeori = jehs / (1 + taxrate / 100);
                    decimal jeinput = 0;
                    for (int m = 0; m <= 25; m++)
                    {
                        var tv = Math.Round(jeori, m);
                        var tv1 = Math.Round(tv * (1 + taxrate / 100), 2);
                        if (tv1 == jehs)
                        {
                            jeinput = tv;
                            break;
                        }
                        else
                        {
                            if (m == 25) { jeinput = tv; }
                        }
                    }
                    var sl = t.iQuantity;
                    var dj = jeinput / sl;
                    detail = xml.CreateElement("Dj");
                    detail.InnerText = dj.ToString();
                    d.AppendChild(detail);
                    detail = xml.CreateElement("Sl");
                    detail.InnerText = sl.ObjToString();
                    d.AppendChild(detail);
                    detail = xml.CreateElement("Je");
                    detail.InnerText = jeinput.ToString();
                    d.AppendChild(detail);
                    detail = xml.CreateElement("Slv");
                    var itaxrate = Math.Round(t.iTaxRateSale.ObjToDecimal() / 100, 2);
                    detail.InnerText = itaxrate.ToString();
                    d.AppendChild(detail);
                    detail = xml.CreateElement("Kce");
                    //detail.InnerText = itaxrate.ToString();
                    d.AppendChild(detail);
                    main.AppendChild(d);
                    irowcount++;
                }
            }
            fp.AppendChild(main);
            fpsj.AppendChild(fp);
            kp.AppendChild(version);
            fpxx.AppendChild(fpsj);
            kp.AppendChild(fpxx);
            xml.AppendChild(kp);
            if (System.IO.File.Exists(savefile))
            { System.IO.File.Delete(savefile); }
            xml.Save(savefile);
            return savefile;
        }
    }
}
